package com.tgu.home_action.controller.filter;

import com.alibaba.fastjson.JSON;
import com.sun.security.auth.UserPrincipal;
import com.tgu.home_action.pojo.exception.RedisTokenErrorException;
import com.tgu.home_action.pojo.exception.RedisTokenNullException;
import com.tgu.home_action.pojo.state.ResponseState;
import com.tgu.home_action.service.utils.JwtService;
import io.jsonwebtoken.ExpiredJwtException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.security.Principal;

@Component
public class SocketInterceptor implements ChannelInterceptor {
    @Autowired
    private JwtService jwtService;

    @Override
    public Message<?> preSend(Message<?> message, MessageChannel channel) {
        StompHeaderAccessor accessor =
                MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
        assert accessor != null;
        if (StompCommand.CONNECT.equals(accessor.getCommand())) {
            String authorization = accessor.getFirstNativeHeader("Authorization");
            if (authorization == null) {
                throw new IllegalArgumentException("101");
            }
            String[] bearer = authorization.split(" ");
            if (!bearer[0].equals("Bearer")) {
                throw new IllegalArgumentException("102");
            }
            String token = bearer[1];
            try {
                if (token.equals("token")) {
                    Principal principal = new UserPrincipal("token");
                    accessor.setUser(principal);
                }else {
                    UserDetails userDetails = jwtService.getUserDetails(token);
                    Principal principal = new UserPrincipal(userDetails.getUsername());
                    accessor.setUser(principal);
                }

            } catch (RedisTokenNullException | ExpiredJwtException e) {
                throw new IllegalArgumentException("103");
            } catch (Exception e) {
                throw new IllegalArgumentException("102");
            }
        }
        return message;
    }

    @Override
    public void afterSendCompletion(Message<?> message, MessageChannel channel, boolean sent, Exception ex) {

    }

    @Override
    public boolean preReceive(MessageChannel channel) {
        return false;
    }

    @Override
    public Message<?> postReceive(Message<?> message, MessageChannel channel) {
        return null;
    }

    @Override
    public void afterReceiveCompletion(Message<?> message, MessageChannel channel, Exception ex) {

    }
}
